package edu.ncsu.csc.camera;

// for SPOT
import com.sun.squawk.util.StringTokenizer;
// for PC
//import java.util.StringTokenizer;


/**
 * This class is an abstraction of the Type H packet.
 * An H packet is a Histogram that can contain 28, 14 or 7 bins
 * It is returned from the GH call (Get Histogram)
 */

public class HPacket
{
	public byte[] bins;
	
	/**
	 * Constructor, expects a String in teh following format:
	 * H bin0 bin1 bin2 ... binN
	 */
	 
	public HPacket(byte[] bins)
	{
		this.bins = bins;
	}
	
	static HPacket build(String str)
	{
		StringTokenizer st = new StringTokenizer(str, " \r");
		int _size = st.countTokens() - 1;	// number of bins
		if(st.nextToken().equals("H"))
		{
			byte[] _bins = new byte[_size];
			for(int k = 0; k < _size; k++)
			{
				_bins[k] = (byte)Integer.parseInt(st.nextToken());
			}
			return new HPacket(_bins);
		}
		return null;
	}

	public String toString()
	{
		String result = "H ";
		int size = bins.length;
		for(int k = 0; k < size; k++)
			result += (0xFF & (int)bins[k]) + " ";	// convert to unsigned byte
		return result;
	}
	
}